<template>
    <div class="cartAll">
        <cart-top :uname="uname"></cart-top>
        <cart-ser></cart-ser>
        <cartlist-top :ischeckAll="ischeckAll" v-on:update_checkAll="update_checkAll"></cartlist-top>
        <cart-list v-for="(elem,i) of msg" :key="i" :count="msg[i].count" :title="msg[i].title" :price="msg[i].price" :img_url="msg[i].img_url" :which="i" :lid="msg[i].id" v-on:count_update="getnum" v-on:check_update="totalUpdate" :ischeckAll="ischeckAll" ></cart-list>
        <div class="zhanwei"></div>
        <total :count="count" :total="total" :ischeckAll="ischeckAll" v-on:update_checkAll="update_checkAll"></total>
    </div>
</template>

<script>
import cartTop from "./components/Carttop.vue"
import cartSer from "./components/CartSer.vue"
import cartList from "./components/Cartlist.vue"
import total from "./components/Total.vue"
import cartlistTop from "./components/CartlistTop.vue"
export default {
    components:{cartSer,cartList,cartTop,total,cartlistTop},
    data(){
        return {
            msg:[],
            uname:"",
            total:0,
            count:0,
            ischeckAll:false
        }
    },
    created(){
        this.axios.get("cart").then(result=>{
            // console.log(result.data);
            var obj = result.data;
            if(obj.code == -1){
                this.$router.push("/login")
            }else{
                this.msg = obj.msg;
                this.uname = this.msg[0].uname;
                // console.log(obj.msg);
                for(var i of obj.msg){
                    this.$store.commit("increatment");
                }
            }
        })
    },
    methods: {
        getnum(obj){
            this.msg[obj.i].count = obj.count;
            // console.log(obj);
            if(obj.ischecked == true){
                
                this.total = this.total + obj.price * obj.updown;
                this.count = this.count + obj.updown;
            }
            
        },
        totalUpdate(obj){
            this.total = this.total + Number(obj.total);
            this.count = this.count + obj.count;
            
        },
        update_checkAll(bol){
            this.ischeckAll = bol ;
        }
    },
}
</script>

<style>
    .cartAll{
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
    }
    .zhanwei{
        height: 80px;
        width: 70%;
        min-width: 800px;
    }
</style>
